<?php

namespace app\common\cachemodel\zlcr3;

use app\common\model\BaseCache;
use XznsTools\cache\CacheManage;

class Zlcr3UserConsultInfo extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zlcr3_user_consult_info';

    protected $cacheModel = 'voice_consult.user_consult_num';

    /**
     * 获取 用户最大咨询对话 次数
     * @param int $uid
     * @return int
     * @throws \Exception
     */
    public function getUserMaxSendNum(int $uid)
    {
        $data = $this->getInfoById($uid);

        if ($data)
        {
            $num = $data['num'];
        } else
        {   // 初始化
            $this->SaveData([
                'uid' => $uid,
                'create_time' => time(),
            ]);

            $num = 0;
        }
        // 用户 剩余咨询次数
        return 40 - $num;
    }

    /**
     * 初始化 咨询会话ID
     * @param array $data
     * @return int
     */
    public function SaveData(array $data): int
    {
        try
        {
            $chatId = $this->insertGetId($data);

            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $data['uid'])->rmCache();
            return (int)$chatId;
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return 0;
        }
    }

    public function updateData($uid, $data)
    {
        try
        {
            $data['update_time'] = time();
            $this->where(['uid' => $uid])->update($data);

            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $uid)->rmCache();
            return true;
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return false;
        }
    }


    /**
     * @param array $ids
     * @param array|null $type
     * @param string $sortType
     * @return array
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        // 获取未删除 的数据
        $where = ['uid' => ['in', $ids]];

        $field = 'uid,num,service_num,last_service_time,create_time,update_time';
        try
        {
            $data = $this
                ->field($field)
                ->where($where)
                ->column($field, 'uid');
        }
        catch (\Exception $e)
        {
            $data = [];
        }
        return $data;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        return $this->dataInit($resData);
    }

    /**
     * 数据初始化
     * @param array $data
     * @return array
     */
    public function dataInit(array $data)
    {
        foreach ($data as $index => $itemData)
        {
            $data[$index] = arrayKeyData2int($itemData, ['uid', 'num', 'service_num', 'last_service_time', 'create_time', 'update_time']);
        }

        return $data;
    }

}